package com.youxin.foundation.utils;

import java.util.HashMap;
import java.util.Map;
/**
 * 转换Bean/JSON/Map对象工具类
 * @author yingde.cao
 *
 */
public class ConvertUtils {
	public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
        if (map == null){
            return null; 
        }
  
        Object obj = beanClass.newInstance();  
  
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
  
        return obj;  
    }    
      
    public static Map<?, ?> objectToMap(Object obj) {  
        if(obj == null){  
            return null;
        }
  
        return new org.apache.commons.beanutils.BeanMap(obj);  
    }  
    
    public static Map<String,String> objectToStringMap(Object obj) {  
        return objectToStringMap(obj, false);
    }  
    
    public static Map<String,String> objectToStringMap(Object obj,boolean useKeylower) {  
        if(obj == null){
            return null;
        }
        
        Map<?, ?> map=new org.apache.commons.beanutils.BeanMap(obj);
        
        Map<String, String> result=new HashMap<>(map.size());
        for(Object key :map.keySet())
        {
        	Object tempObj=map.get(key);
        	if(tempObj==null){
        		continue;
        	}
        	if("class".equals(key)){
        		continue;
        	}
        	String tempValue=String.valueOf(tempObj);
        	if(useKeylower){
        		result.put(key.toString().toLowerCase(), tempValue);
        	}else{
        		result.put(key.toString(), tempValue);
        	}
        }
        
        return result;
    } 
}
